package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/9/27.
 */
public class LocalActivity1 extends Activity implements View.OnClickListener,OnNetResultListener{
    private MarriageProposalBean.DataBean dataBean;
    //title
    private TextView title_tv;
    //输入框
    private EditText editText;
    //点击完成
    private TextView laocal_place_save;
    //输入的地址;
    private String content;
    //标题
    private String title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job_edit_pager_location);
        dataBean = (MarriageProposalBean.DataBean) getIntent().getSerializableExtra(JobDetail.TASK_FLAG);
        title = dataBean.getBookTitle();
        initView();
        initData(dataBean);

    }

    private void initView() {
        title_tv = (TextView) findViewById(R.id.title_tv);
        editText = (EditText) findViewById(R.id.local_place_editext);
        laocal_place_save = (TextView) findViewById(R.id.laocal_place_save);
        laocal_place_save.setOnClickListener(this);
    }

    private void initData(MarriageProposalBean.DataBean dataBean) {
        title_tv.setText(title);
        if (!TextUtils.isEmpty(dataBean.getBookPlace())) {
            editText.setText(dataBean.getBookPlace());
        }
    }


    public void back(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        sendData();
    }

    @Override
    public void onClick(View v) {
        content = editText.getText().toString();
        switch (v.getId()){
            case R.id.laocal_place_save:
                getChangePlace();
                dataBean.setBookPlace(content);
                break;
        }
    }


    private  void  getChangePlace(){
       // String userurl = UrlDealUtil.getRealUrl(RequestCode.UPDATE_PLACE );
        String userurl = UrlDealUtil.getRealUrlNoId(RequestCode.UPDATE_PLACE)+"&i_Id="+dataBean.getMemberID();
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("BookPlace",content);
        paramsMap.put("BookTitle",title);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, userurl, this, CommonResultBean.class, RequestCode.UPDATE_PLACE, paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);
    }


    private CommonResultBean bean;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data =  sucessbean.getData();
        if (data instanceof CommonResultBean) {
            bean = (CommonResultBean) data;
            switch (bean.getResCode()) {
                case "0000":
                    //设置适配器
                    CUtils.showToast("添加成功了");
                    sendData();
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    private void sendData(){
        Intent intent = new Intent();
        intent.putExtra(JobDetail.TASK_FLAG,dataBean);
        setResult(RESULT_OK,intent);
        finish();
    }
}
